package com.ruoyi.wxmenu;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.wx.domain.ACat;
import com.ruoyi.wx.domain.ACount;
import com.ruoyi.wx.domain.ADaily;
import com.ruoyi.wx.domain.AModule;
import com.ruoyi.wx.service.IACatService;
import com.ruoyi.wx.service.IACountService;
import com.ruoyi.wx.service.IADailyService;
import com.ruoyi.wx.service.IAModuleService;
import com.ruoyi.wx.service.impl.ACountServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.ruoyi.wx.vo.vDataCount;

/**
 * author：pan
 * 微信小程序  排行榜
 *
 */

@RestController
@RequestMapping("/wxapi")
public class WxRankController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private IAModuleService moduleService;

    @Autowired
    private IACountService countService;

    @Autowired
    private IACatService catService;

    @Autowired
    private IADailyService dailyService;

    /**
     * 查询排名前10的猫咪热度
     * @return
     */
    @GetMapping("/getHotRanks")
    public AjaxResult getHotRanks(){
        // Redis的key
        String key = "load:rank:hot";
        // 去Redis缓存中查找
        String moduleListJson = redisTemplate.opsForValue().get(key);

        // 如果缓存中存在
        if(StrUtil.isNotBlank(moduleListJson)){

            //将数据转换
            List<ACat> aCatList = JSONUtil.toList(moduleListJson, ACat.class);

            //直接返回数据【缓存】
            return AjaxResult.success("缓存:排行查询成功！",aCatList);
        }


        // 如果没有则进数据库去查找
        // 先从count表中获取前十的排名
        List<ACount> counts = countService.getHotRanks();

        // 用id去获取猫咪的头像和昵称

        List<ACat> cats = new ArrayList<>();

        for (ACount count : counts){
            long id = count.getId();
            ACat cat = catService.selectACatById(id);
            cat.setId(count.getHot());
            cats.add(cat);
        }

        // 设置缓存【60min】
        redisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(cats),60, TimeUnit.MINUTES);


        return AjaxResult.success("排行查询成功！", cats);
    }

    /**
     * 查询排名前10的猫咪热度
     * @return
     */
    @GetMapping("/getDataCount")
    public AjaxResult getDataCount(){

        // Redis的key
        String key = "load:rank:count";
        // 去Redis缓存中查找
        String moduleListJson = redisTemplate.opsForValue().get(key);

        // 如果缓存中存在
        if(StrUtil.isNotBlank(moduleListJson)){

            //获取redis数据
            vDataCount dataCount = JSONUtil.toBean(moduleListJson, vDataCount.class);

            //直接返回数据【已设置 永不过期】
            return AjaxResult.success("缓存:统计信息查询成功！",dataCount);
        }

        // 如果没有则进数据库去查找
        vDataCount dataCount = new vDataCount();

        // 查询猫咪在读数
        Long catCount = catService.selectACatCount();

        // 查询猫咪日常总数
        Long catDailys = dailyService.selectADailyCount();

        // 放数据
        dataCount.setAllCat(catCount);   // 猫咪在读数
        dataCount.setAllDaily(catDailys);  // 猫咪日常总数


        // 写入缓存
        redisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(dataCount),60, TimeUnit.MINUTES);

        return AjaxResult.success("排行查询成功！", dataCount);
    }


}
